/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * KursFrame.java
 *
 * Created on 2006-01-01, 22:23:08
 */
package bd2;

import javax.swing.JOptionPane;
import java.util.Date;
import java.util.ArrayList;

/**
 *
 * @author tolkjen
 */
public class KursFrame extends javax.swing.JFrame implements SignalReciever {
    
    private KategorieFrame katFrame;
    private WykladowcyFrame wyklFrame;
    private OsobyFrame osobyFrame;
    private SignalReciever reciever;
    private Integer idKurs, idKat;
    private String nrWykladowcy;

    /** Creates new form KursFrame */
    public KursFrame(SignalReciever sr) {
        initComponents();
        katFrame = new KategorieFrame();
        wyklFrame = new WykladowcyFrame();
        osobyFrame = new OsobyFrame();
        
        reciever = sr;
        idKat = -1;
        idKurs = -1;
        nrWykladowcy = "";
    }
    
    @Override
    public void signal(String typeName, Object[] data) {
        if (typeName.equals("kategoria")) {
            idKat = (Integer) data[0];
            jTextField1.setText((String) data[1]);
            
            // clear 'wykladowca'
            nrWykladowcy = "";
            jTextField5.setText("");
        }
        else if (typeName.equals("wykładowca")) {
            nrWykladowcy = (String) data[0];
            jTextField5.setText((String) data[1]);
        }
        else if (typeName.equals("kursant")) {
            Integer idOsoby = (Integer) data[0];
            
            DataStore ds = DataStore.getInstance();
            if (ds.addOsobaToKurs(idKurs, idOsoby)) {
                loadKursanci();
            } else {
                JOptionPane.showMessageDialog(null, 
                    "Nie udało się dodać osoby do kursu!",
                    "Program", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    
    public void show(int id) {
        idKurs = id;
        
        if (!loadKurs()) {
            JOptionPane.showMessageDialog(null, 
                    "Nie udało się pobrać danych kursu z bazy!",
                    "Program", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if (!loadKursanci()) {
            JOptionPane.showMessageDialog(null, 
                    "Nie udało się pobrać danych kursantów z bazy!",
                    "Program", JOptionPane.ERROR_MESSAGE);
            return ;
        }
        
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    private Boolean loadKurs() {
        DataStore ds = DataStore.getInstance();
        Object[] data = ds.getKurs(idKurs);
        
        if (data != null) {
            // set field values
            jFormattedTextField1.setValue(data[6]);
            jSpinner2.setValue(data[4]);
            jSpinner1.setValue(data[5]);
        
            // set 'wykladowca' field
            nrWykladowcy = (String) data[3];
            jTextField5.setText((String) data[2]);
        
            // set 'kategoria' field
            idKat = (Integer) data[1];
            jTextField1.setText((String) data[0]);
            
            return true;
        } else {
            return false;
        }
    }
    
    private Boolean loadKursanci() {
        DataStore ds = DataStore.getInstance();
        ArrayList<Object[]> data = ds.getKursanci(idKurs);
        if (data != null) {
            kursanciTable1.clear();
            for (int i=0; i<data.size(); i++)
                kursanciTable1.addRow(data.get(i));
            kursanciTable1.updateUI();
            
            return true;
        } else {
            return false;
        }
    }
    
    private Boolean checkFields() {
        Double cost = Double.valueOf(jFormattedTextField1.getText().replace(",", "."));
        return (idKurs != -1 && !nrWykladowcy.equals("") && cost >= 0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jSpinner2 = new javax.swing.JSpinner();
        jSpinner1 = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        kursanciTable1 = new bd2.KursanciTable();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setTitle("Informacje o kursie");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Kategoria");

        jLabel2.setText("Cena");

        jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        jFormattedTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jButton3.setText("...");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTextField1.setEditable(false);

        jLabel3.setText("Rozpoczęcie");

        jSpinner2.setModel(new javax.swing.SpinnerDateModel());

        jSpinner1.setModel(new javax.swing.SpinnerDateModel());

        jLabel4.setText("Zakończenie");

        jLabel5.setText("Prowadzący");

        jTextField5.setEditable(false);

        jButton4.setText("...");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton2.setText("Zapisz zmiany");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jTextField5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton4))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton3))
                                .addComponent(jLabel1)
                                .addComponent(jLabel3)
                                .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2)
                                    .addComponent(jSpinner1, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))
                                .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jButton2))
                .addContainerGap(286, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3)
                    .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addContainerGap(130, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Dane kursu", jPanel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jScrollPane1.setViewportView(kursanciTable1);

        jButton5.setText("Dodaj kursanta");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Usuń kursanta");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Dodaj wpłatę");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton7)
                .addContainerGap(306, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 643, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jButton6)
                    .addComponent(jButton7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Uczestnicy", jPanel2);

        jButton1.setText("Powrót");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(586, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 659, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        katFrame.show(this);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        wyklFrame.setKategoria(idKat);
        wyklFrame.show(this);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (!checkFields()) {
            JOptionPane.showMessageDialog(null, 
                    "Wypełnij prawidłowo wszystkie pola!",
                    "Program", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        
        Date start = (Date) jSpinner2.getValue();
        Date finish = (Date) jSpinner1.getValue();
        Double cost = Double.valueOf(jFormattedTextField1.getText().replace(",", "."));
        
        DataStore ds = DataStore.getInstance();
        if (ds.updateKurs(idKurs, idKat, nrWykladowcy, start, finish, cost))
            JOptionPane.showMessageDialog(null, "Zapisano zmiany!",
                    "Program", JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(null, "Błąd podczas dodawania danych do bazy!",
                    "Program", JOptionPane.ERROR_MESSAGE);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        osobyFrame.setKurs(idKurs);
        osobyFrame.show(this);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        if (kursanciTable1.getSelectedRowCount() > 0) {
            Integer row = kursanciTable1.getSelectedRow();
            Integer Id = (Integer) kursanciTable1.getRow(row)[0];
            
            DataStore ds = DataStore.getInstance();
            if (ds.removeOsobaFromKurs(idKurs, Id)) {
                loadKursanci();
            } else {
                JOptionPane.showMessageDialog(null, 
                        "Błąd podczas usuwania kursanta!",
                        "Program", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Wybierz kursanta!",
                    "Program", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        if (kursanciTable1.getSelectedRowCount() > 0) {
            Integer row = kursanciTable1.getSelectedRow();
            Integer idOsoba = (Integer) kursanciTable1.getRow(row)[0];
            
            String value = JOptionPane.showInputDialog(null, "Podaj wysokość wpłaty", 0);
            
            try {
                Double cash = Double.parseDouble(value);
                DataStore ds = DataStore.getInstance();
                if (ds.addWplata(idKurs, idOsoba, cash)) {
                    loadKursanci();
                } else {
                    JOptionPane.showMessageDialog(null, 
                        "Błąd przy dodawaniu wpłaty!",
                        "Program", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, 
                        "Wprowadź wartość liczbową!",
                        "Program", JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Wybierz kursanta!",
                    "Program", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButton7ActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField5;
    private bd2.KursanciTable kursanciTable1;
    // End of variables declaration//GEN-END:variables
}
